package com.ljl.ningwei;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;
import lombok.Data;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Collections;

/**
 * @author 吕佳乐
 * @version 1.0
 * @date 2025-03-23 17:29
 */
public class Main {
    public static void main(String[] args) {
        // 手动加载 MySQL 驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to load MySQL JDBC driver");
        }
        FastAutoGenerator.create("jdbc:mysql://111.230.81.26:3306/ningwei?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8"
                        , "root", "lvjialeyeezy0426")
                .globalConfig(builder -> builder
                                .author("ljl")
                                .outputDir(Paths.get("").toAbsolutePath().toString() + "/generator" + "/src/main/java")
//                        .commentDate("yyyy-MM-dd")
                )
                .dataSourceConfig(builder ->
                                builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                                    // 打印字段类型信息
                                    System.out.println("字段类型: " + metaInfo.getJdbcType() + " → Java类型: " + typeRegistry.getColumnType(metaInfo));
                                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                                    // 自定义映射逻辑
                                    if (typeCode == Types.SMALLINT || typeCode == Types.TINYINT) {
                                        return DbColumnType.INTEGER;
                                    } else if (typeCode == Types.DATE) {
                                        return DbColumnType.LOCAL_DATE;
                                    } else if (typeCode == Types.TIMESTAMP) {
                                        return DbColumnType.LOCAL_DATE_TIME;
                                    } else if (typeCode == Types.VARCHAR || typeCode == Types.CLOB) {
                                        return DbColumnType.STRING;
                                    } else if (typeCode == Types.BIT) {
                                        return DbColumnType.BOOLEAN;
                                    }
                                    return typeRegistry.getColumnType(metaInfo);
                                })
                )
                .packageConfig(builder ->
                        builder.parent("com.ljl")
                                .moduleName("ningwei") // 设置父包名
                                .pathInfo(Collections.singletonMap(OutputFile.xml,
                                        Paths.get("").toAbsolutePath().toString() + "/generator/src/main/resources")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude("category")
                                // 禁用不需要生成的组件
                                .entityBuilder()
                                .enableLombok(new ClassAnnotationAttributes("@Data","lombok.Data"))
                                .enableLombok(new ClassAnnotationAttributes("@Builder","lombok.Builder"))
                                .controllerBuilder().disable() // 禁用Controller
                                .mapperBuilder()
                                .mapperXmlTemplate("templates/mapper.xml.vm")
                                .serviceBuilder()
                                .serviceTemplate("templates/service.java.vm")
                )
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }

}
